package com.panel;


import com.panel.rect.CreateRect;


import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;





/**
 * @author dell
 */
public class HeroPanel extends JPanel {
    //创建游戏窗口   窗口中应有背景 人物 方块图形等元素
    //声明（组件）
//    背景图
    //生命
    private int life;
    Boolean b = true;
    private BufferedImage backgroundimage;
    //    人物图
    private BufferedImage hero;
    //    判断是否开始
    private Boolean start;
    //    判断是否结束
    private Boolean end;
    //    判断是否划线状态
    private Boolean drawline;
    //    判断是否旋转线状态
    private Boolean revoelline;

    //     判断是否人物移动状态
    private Boolean herorun;
    //判断背景是否移动
    private Boolean bkrun;
    //限制  一条水平线
    private static final int height = 474;
    //划线长度
    private int lengthline;
    //旋转线时的x坐标
    private int xline;
    //旋转线时的 y坐标；
    private int yline;
    //人物位置
    private int heropoint;
    //人物下落
    private int herodrop;
    //分数
    private int score;
    //人物宽度和 高度
    private int herowidth;
    private int heroheight;
    //游戏界面中一共可以出现两个矩形
    private int[] firstrect;
    private int[] secondrect;
    // 线程变量  控制游戏
    private Thread thread;

    // 构造函数
    public HeroPanel() {
        try {
            //初始化 准备（生产）组件
            backgroundimage = ImageIO.read(new FileInputStream("F:\\bk.jpg"));
            hero = ImageIO.read(new FileInputStream("F:\\hero.jpg"));
            start = false;
            end = false;
            drawline = false;
            revoelline = false;
            herorun = false;
            bkrun = false;
            lengthline = 0;
            xline = 0;
            yline = 0;
            score = 0;
            heropoint = 80;
            herodrop = 0;
            herowidth = hero.getWidth();
            heroheight = hero.getHeight();
            firstrect = new int[]{0, 80};
            //创建第一个块
            secondrect = new int[2];
            secondrect = CreateRect.init();
            // 设置鼠标监听
            addMouseListener(new MyMouseListenter());
        } catch (IOException e) {
            System.out.println("-==================================");
            e.printStackTrace();
        }
    }

    //绘制界面  函数  各种界面
    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        //游戏开始运行
        // 把背景图像放入
        graphics.drawImage(backgroundimage, 0, 0, null);
        //创建字体对象
        Font font = new Font("宋体", Font.BOLD, 30);
        graphics.setFont(font);
        //当游戏还没有开始的时候  执行
        if (!start && !end) {
            graphics.drawString("开始游戏", 140, 300);

        }
        //游戏开始了  但还没进行操作 背景没动  部署组件
        if (start && !bkrun) {
            graphics.fillRect(firstrect[0], height, firstrect[1] - firstrect[0], 200);
            graphics.fillRect(secondrect[0], height, secondrect[1] - secondrect[0], 200);
            //显示分数区域
            graphics.drawString("分数：" + score, 150, 70);
        }
        //部署人物  开始位置  站在边缘
        if (start && !herorun) {
            graphics.drawImage(hero, heropoint - herowidth, height - heroheight, null);

        }
        //划线操作  两点一线 增长
        if (drawline) {
            graphics.drawLine(80, height, 80, height - lengthline);
        }
        //旋转   线  height 线上方
        if (revoelline) {
            graphics.drawLine(80, height, 80 + xline, height - yline);
        }
        //人物移动
        if (herorun) {
            graphics.drawLine(80, height, 80 + lengthline, height);
            graphics.drawImage(hero, heropoint - herowidth, height - heroheight, null);
        }
        //背景有变化  背景移动  矩形线条等
        if (bkrun) {
            graphics.fillRect(firstrect[0], height, firstrect[1] - firstrect[0], 200);
            graphics.fillRect(secondrect[0], height, secondrect[1] - secondrect[0], 200);
            graphics.drawImage(hero, heropoint - herowidth, height - heroheight, null);
            graphics.drawString("分数：" + score, 150, 70);
        }
        //游戏结束 但没开始 就是死了。
        if (!start && end) {

            graphics.fillRect(firstrect[0], height, firstrect[1] - firstrect[0], 200);
            graphics.fillRect(secondrect[0], height, secondrect[1] - secondrect[0], 200);
            graphics.drawString("分数：" + score, 150, 70);

            if (life == 1) {
                graphics.drawString("游戏结束", 140, 300);


            }
            else if(life ==0){
                graphics.drawString("复活", 140, 300);
            }

            graphics.drawImage(hero, heropoint - herowidth, height - heroheight + 20 * herodrop++, null);
            graphics.drawLine(80, height, 80 + xline, height + yline);

        }
    }

    //鼠标监听
    class MyMouseListenter extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            if (start) {
                drawline = true;
                // 主线程负责显示界面 人物移动  旋转 增长  要其他线程完成
                thread = new Thread(new extendLine());
                thread.start();
            }
        }

        //        点击松手才会执行  不松手不执行
        @Override
        public void mouseReleased(MouseEvent e) {
            if (start) {
                drawline = false;
                thread = new Thread(new revoleLine());
                revoelline = true;
                yline = lengthline;
                thread.start();

            } else {
                start = true;
                end = false;
                repaint();
            }
        }
    }

    //增长棍子
    class extendLine implements Runnable {

        @Override
        public void run() {
            while (drawline) {
                lengthline++;
                repaint();
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //旋转棍子  xy数据变化
    class revoleLine implements Runnable {

        @Override
        public void run() {
            while (xline <= lengthline) {
                xline++;
                int num = lengthline * lengthline - xline * xline;
                yline = (int) Math.sqrt(num);
                repaint();
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            revoelline = false;
            herorun = true;
            thread = new Thread(new heroRun());
            thread.start();
        }
    }

    //人物移动
    class heroRun implements Runnable {

        @Override
        public void run() {
            //棍子的头的坐标
            int p = lengthline + 80;
            int l = p;
            //如果棍子头在 第二个矩形块中
            if (p >= secondrect[0] && p <= secondrect[1]) {
                //将会跑到   第二个块头
                l = secondrect[1];
            }
            //进行跑动
            while (heropoint <= l) {
//                heropoint++;
                heropoint = heropoint + 5;
                repaint();
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //跑完  判断是否游戏结束
            herorun = false;
            //判断
            if (l == secondrect[1]) {
                //成功
                thread = new Thread(new moveBk());
                bkrun = true;
                score++;
                thread.start();
            } else {
                //失败
                start = false;
                end = true;
                //开启失败动态
                thread = new Thread(new gameOver());
                thread.start();
            }
        }
    }

    //背景移动
    class moveBk implements Runnable {

        @Override
        public void run() {
            while (secondrect[1] > 80) {
                secondrect[1]--;
                secondrect[0]--;
                firstrect[0]--;
                firstrect[1]--;
                heropoint--;
                repaint();
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            bkrun = false;

            //背景移动完毕  重置数据
            firstrect = secondrect;
            secondrect = CreateRect.init();
//            此数据用于数据变化的增量

            lengthline = 0;
            xline = 0;
            yline = 0;
            heropoint = 80;
            repaint();
        }
    }

    //游戏结束
    class gameOver implements Runnable {

        @Override
        public void run() {
            while (xline > 0) {
                xline--;
                int num = lengthline * lengthline - xline * xline;
                yline = (int) Math.sqrt(num);
                repaint();
                try {
                    Thread.sleep(5); 
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //清空数据

              if(life==1) {
                  lengthline = 0;
                  xline = 0;
                  yline = 0;
                  score = 0;
                  life = 0;
                  b=true;
              }
              else{
                  life++;
              }
                  heropoint = 80;
                  herodrop = 0;
                  firstrect = new int[]{0, 80};
                  secondrect = CreateRect.init();









        }

    }
}
